package dao;

import pojo.QualifiedProduct;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class QualifiedProductDAOImpl {

    private static final String TABLE_NAME = "qualified_product";

    public static void add(QualifiedProduct qualifiedProduct) throws SQLException {
        String sql = "INSERT INTO " + TABLE_NAME + " VALUES (?, ?)";
        JDBCUtils.execute(sql, null, qualifiedProduct.getItemNumber());
    }

    public static void delete(int approvedNumber) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE approved_number = ?";
        JDBCUtils.execute(sql, approvedNumber);
    }

    public static void update(QualifiedProduct qualifiedProduct) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET item_number = ? WHERE approved_number = ?";
        JDBCUtils.execute(sql, qualifiedProduct.getItemNumber(), qualifiedProduct.getApprovedNumber());
    }

    public static QualifiedProduct getById(int approvedNumber) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE approved_number = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, approvedNumber);
        if (resultSet.next()) {
            return createQualifiedProductFromResultSet(resultSet);
        }
        return null;
    }

    public static List<QualifiedProduct> getAll() throws SQLException {
        List<QualifiedProduct> qualifiedProducts = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        while (resultSet.next()) {
            QualifiedProduct qualifiedProduct = createQualifiedProductFromResultSet(resultSet);
            qualifiedProducts.add(qualifiedProduct);
        }
        return qualifiedProducts;
    }

    public static int getCount() throws SQLException {
        String sql = "SELECT COUNT(*) AS count FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        if (resultSet.next()) {
            return resultSet.getInt("count");
        }
        return 0;
    }

    public static List<QualifiedProduct> getByPage(int start, int pageSize) throws SQLException {
        List<QualifiedProduct> qualifiedProducts = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, start, pageSize);
        while (resultSet.next()) {
            QualifiedProduct qualifiedProduct = createQualifiedProductFromResultSet(resultSet);
            qualifiedProducts.add(qualifiedProduct);
        }
        return qualifiedProducts;
    }

    public static List<QualifiedProduct> findByKeyword(String keyword) throws SQLException {
        List<QualifiedProduct> qualifiedProducts = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE item_number LIKE ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%");
        while (resultSet.next()) {
            QualifiedProduct qualifiedProduct = createQualifiedProductFromResultSet(resultSet);
            qualifiedProducts.add(qualifiedProduct);
        }
        return qualifiedProducts;
    }

    public static List<QualifiedProduct> findByPageAndKeyword(int start, int pageSize, String keyword) throws SQLException {
        List<QualifiedProduct> qualifiedProducts = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE item_number LIKE ? LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%", start, pageSize);
        while (resultSet.next()) {
            QualifiedProduct qualifiedProduct = createQualifiedProductFromResultSet(resultSet);
            qualifiedProducts.add(qualifiedProduct);
        }
        return qualifiedProducts;
    }

    private static QualifiedProduct createQualifiedProductFromResultSet(ResultSet resultSet) throws SQLException {
        int approvedNumber = resultSet.getInt("approved_number");
        int itemNumber = resultSet.getInt("item_number");
        return new QualifiedProduct(approvedNumber, itemNumber);
    }
}
